/**
 * 根据数据库的城市信息及其连接信息，求其邻接矩阵
 * @param {Array} citys  [{id:...,city:...},{},...]
 * @param {Array} cityPath ：[{id:...,city1:...,city2:...,price:...,pathlen:...},{},....]
 * @returns 
 * @author majx
 */
var findPath = require("./FindPath");
var Weight = findPath.Weight;

var getGraph = (citys,cityPath)=>{ 
    let cityNum = citys.length;//城市个数
    
    let graph = [];

    let map = new Map();
    for(let i = 0;i < citys.length;i++){ 
        map.set(citys[i].id,i);
    }

    for(let i = 0; i < cityNum;i++){ 
        let item = [];
        for(let j = 0; j < cityNum;j++){ 
            let weight = new Weight(0,-1);
            if(i == j){
                weight.pathLength = 0;
            }

            item.push(weight);
        }
        graph.push(item);
    }

    for(let i = 0; i < cityPath.length;i++){ 
        let start = map.get(cityPath[i].city1);
        let target = map.get(cityPath[i].city2);
        let weight = new Weight(cityPath[i].price,cityPath[i].pathLength);
        graph[start][target] = weight;
        graph[target][start] = weight;
    }

    return graph;
}

module.exports.getGraph = getGraph;